//
//  FreightVC.m
//  saler-ios
//
//  Created by 王亚陆 on 17/6/7.
//  Copyright © 2017年 sayimba. All rights reserved.
//

#import "FreightVC.h"
#import "EndAddressVC.h"
#import "FreightDetailVC.h"
#import "BegainAddressVC.h"
#import "BegainAddressChooseVC.h"
#import "EndAddressChooseVC.h"
@interface FreightVC ()
{
    UIImageView * nav;
    RootViewController * TabBar;
    NSMutableDictionary * callBackDic;

    UILabel * begainAddressLabel;
    UILabel * endAddressLabel;
    NSString * begainID;
    NSString * endID;
}

@property (nonatomic,strong) UIView * backview;
@end

@implementation FreightVC

- (void)viewDidLoad {
    [super viewDidLoad];
    nav = [self createNav:@"运价查询"];
    [self.view addSubview:nav];
    self.view.backgroundColor = UIviewBackColor;
    
    [self createBackview];
    [self createBtn];
    
}



-(void)createBackview{
    _backview = [[UIView alloc]init];
    _backview.backgroundColor = WhiteColor;
    [self.view addSubview:_backview];
    [_backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(nav.mas_bottom).with.offset(10*kHeight);
        make.size.mas_equalTo(CGSizeMake(view_Width, 190*kHeight));
    }];
    
    
    UIImageView * addressImage = [[UIImageView alloc]init];
    addressImage.image = [UIImage imageNamed:@"home_address"];
    [_backview addSubview:addressImage];
    [addressImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backview.mas_left).with.offset(24*kWidth);
        make.top.mas_equalTo(_backview.mas_top).with.offset(25*kHeight);
        make.size.mas_equalTo(CGSizeMake(10*kWidth, 15*kHeight));
    }];
    
    UILabel * addressLabel = [UILabel createUIlabel:@"运输地址" andFont:14 andColor:UIlabelColor];
    [_backview addSubview:addressLabel];
    [addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(addressImage.mas_right).with.offset(22*kWidth);
        make.centerY.mas_equalTo(addressImage.mas_centerY);
    }];
    
    UIImageView * begainAddressImage = [[UIImageView alloc]init];
    begainAddressImage.image = [UIImage imageNamed:@"home_yellow"];
    [_backview addSubview:begainAddressImage];
    [begainAddressImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backview.mas_left).with.offset(22*kWidth);
        make.top.mas_equalTo(addressImage.mas_bottom).with.offset(45*kHeight);
        make.size.mas_equalTo(CGSizeMake(6*kWidth, 6*kHeight));
    }];
    
    begainAddressLabel = [UILabel createUIlabel:@"始发地" andFont:14 andColor:UIbtnColor];
    [_backview addSubview:begainAddressLabel];
    [begainAddressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(addressLabel.mas_left);
        make.centerY.mas_equalTo(begainAddressImage.mas_centerY);
    }];
    
    UIImageView *  chooseImage1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"home_choose"]];
    [_backview addSubview:chooseImage1];
    [chooseImage1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(begainAddressLabel.mas_right).with.offset(10*kWidth);
        make.centerY.mas_equalTo(begainAddressLabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(8*kWidth, 5*kHeight));
    }];
    
    
    UIButton * begainBtn = [UIButton createUIbutton:@"" andFont:1 andColor:nil];
    [begainBtn addTarget:self action:@selector(pressBegainBtn) forControlEvents:UIControlEventTouchUpInside];
    [_backview addSubview:begainBtn];
    [begainBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(addressLabel.mas_left);
        make.centerY.mas_equalTo(begainAddressImage.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(200*kWidth, 40*kHeight));
    }];
    
    
    UIView * lineView1= [[UIView alloc]init];
    lineView1.backgroundColor = LineColor;
    [_backview addSubview:lineView1];
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(begainAddressLabel.mas_left);
        make.centerY.mas_equalTo(begainAddressLabel.mas_centerY).with.offset(15*kHeight);
        make.size.mas_equalTo(CGSizeMake(lineView_Width, 0.5*kHeight));
    }];
    

    UIImageView * endAddressImage = [[UIImageView alloc]init];
    endAddressImage.image = [UIImage imageNamed:@"home_blue"];
    [_backview addSubview:endAddressImage];
    [endAddressImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backview.mas_left).with.offset(24*kWidth);
        make.top.mas_equalTo(begainAddressImage.mas_bottom).with.offset(55*kHeight);
        make.size.mas_equalTo(CGSizeMake(6*kWidth, 6*kHeight));
    }];
    
    endAddressLabel = [UILabel createUIlabel:@"目的地" andFont:14 andColor:UIbtnColor];
    [_backview addSubview:endAddressLabel];
    [endAddressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(addressLabel.mas_left);
        make.centerY.mas_equalTo(endAddressImage.mas_centerY);
    }];
    UIImageView *  chooseImage2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"home_choose"]];
    [_backview addSubview:chooseImage2];
    [chooseImage2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(endAddressLabel.mas_right).with.offset(10*kWidth);
        make.centerY.mas_equalTo(endAddressLabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(8*kWidth, 5*kHeight));
    }];
    
    UIButton * endBtn = [UIButton createUIbutton:@"" andFont:1 andColor:nil];
    [endBtn addTarget:self action:@selector(pressEndBtn) forControlEvents:UIControlEventTouchUpInside];
    [_backview addSubview:endBtn];
    [endBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(endAddressLabel.mas_left);
        make.centerY.mas_equalTo(endAddressImage.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(200*kWidth, 40*kHeight));
    }];
    
    UIView * lineView2= [[UIView alloc]init];
    lineView2.backgroundColor = LineColor;
    [_backview addSubview:lineView2];
    [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(endAddressLabel.mas_left);
        make.centerY.mas_equalTo(endAddressLabel.mas_centerY).with.offset(15*kHeight);
        make.size.mas_equalTo(CGSizeMake(lineView_Width, 0.5*kHeight));
    }];
}


-(void)pressBegainBtn{
    
    
    BegainAddressChooseVC * address = [[BegainAddressChooseVC alloc]init];
    address.callBackAddress = ^(NSMutableDictionary * dic){
        
        begainAddressLabel.text = [NSString stringWithFormat:@"%@",dic[@"sname"]];
        begainID = dic[@"id"];
        begainAddressLabel.textColor = littleBlackColor;
        
        NSString * end = [NSString stringWithFormat:@"%@",endID];
        if (end.length >0) {
            
            endID = nil;

            endAddressLabel.text = @"目的地";
            endAddressLabel.textColor = UIbtnColor;
        }
    };
    [self.navigationController pushViewController:address animated:YES];
  
}

-(void)pressEndBtn{
    
    
    NSString * address = [NSString stringWithFormat:@"%@",begainAddressLabel.text];
    if ([address isEqualToString:@"始发地"]) {
        
        [self createUIAlertController:@"请先选择始发地"];
    }else{
        EndAddressChooseVC * endaddress = [[EndAddressChooseVC alloc]init];
        endaddress.callBackAddress = ^(NSMutableDictionary * dic){
            endAddressLabel.text = [NSString stringWithFormat:@"%@",dic[@"sname"]];
            endAddressLabel.textColor = littleBlackColor;

            endID = dic[@"id"];
            
        };
        endaddress.begainID = begainID;
        [self.navigationController pushViewController:endaddress animated:YES];
    }
}



-(void)createBtn{
    
    
    UIButton * nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextBtn setTitle:@"查询" forState:UIControlStateNormal];
    //    [nextBtn setImage:[UIImage imageNamed:@"home_next"] forState:UIControlStateNormal];
    [nextBtn setTitleColor:WhiteColor forState:UIControlStateNormal];
    [nextBtn addTarget:self action:@selector(pressNextBtn:) forControlEvents:UIControlEventTouchUpInside];
    nextBtn.layer.cornerRadius = 5;
    nextBtn.layer.borderWidth = 0.5;
//    nextBtn.layer.borderColor = UIbtnColor.CGColor;
//    nextBtn.backgroundColor = UIbtnColor;
    nextBtn.layer.borderColor = YellowColor.CGColor;
    nextBtn.backgroundColor = YellowColor;

    [self.view addSubview:nextBtn];
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(355*kWidth, 50*kHeight));
        make.top.mas_equalTo(_backview.mas_bottom).with.offset(37*kHeight);
    }];
}


-(void)pressNextBtn:(UIButton*)sender{
    

//    Order_price
    
    NSString *urlString = [NSString stringWithFormat:@"%@/%@/%@",Order_price,begainID,endID];
    
    [Common requestWithUrlString:urlString contentType:application_json finished:^(id responseObj) {
        
        NSLog(@"%@",responseObj[@"message"]);
        if ([responseObj[@"success"] boolValue]) {
            
            
            FreightDetailVC * detail = [[FreightDetailVC alloc]init];
            detail.inforDic = responseObj[@"data"];
            [self.navigationController pushViewController:detail animated:YES];
            NSLog(@"下一步");
            
        } else {
            
            [WKProgressHUD popMessage:responseObj[@"message"] inView:self.view duration:1.5 animated:YES];
            
        }
        
    } failed:^(NSString *errorMsg) {
        
        NSLog(@"%@",errorMsg);
        
    }];

}


-(void)createUIAlertController:(NSString*)title

{
    UIAlertController * alert =[UIAlertController alertControllerWithTitle:@"提示" message:title preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * action =[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction * action1 =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:action1];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)viewWillAppear:(BOOL)animated{
    
    RootViewController *rootVC = [RootViewController defaultsTabBar];
    [rootVC setTabBarHidden:YES];
    
}
@end
